package cn.itcast.controller;

import cn.itcast.domain.Msg;
import cn.itcast.domain.User;
import cn.itcast.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    public Msg updatePwd(@RequestParam("password")String password){
        //修改redis缓存，并获取id
        User currentUser = (User) redisTemplate.opsForHash().get("token", "user");
        currentUser.setPassword(password);
        redisTemplate.opsForHash().put("token","user",currentUser);
        Integer id = currentUser.getId();

        //调用service层方法修改password
        userService.updatePwd(password,id);

        return Msg.success();
    }
    @RequestMapping(value = "/updateSex", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateSex(@RequestParam("sex")String sex){
        //修改redis缓存，并获取id
        User currentUser = (User) redisTemplate.opsForHash().get("token", "user");
        currentUser.setSex(sex);
        redisTemplate.opsForHash().put("token","user",currentUser);
        Integer id = currentUser.getId();

        //调用service层方法修改sex
        userService.updateSex(sex,id);

        return Msg.success();
    }

    @RequestMapping(value = "/updateEmail", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateEmail(@RequestParam("email")String email){
        //修改redis缓存，并获取id
        User currentUser = (User) redisTemplate.opsForHash().get("token", "user");
        currentUser.setEmail(email);
        redisTemplate.opsForHash().put("token","user",currentUser);
        Integer id = currentUser.getId();

        //调用service层方法修改email
        userService.updateEmail(email,id);

        return Msg.success();
    }

    @RequestMapping(value = "/updateBirthday", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateBirthday(@RequestParam("birthday")String birthday){
        //修改redis缓存，并获取id
        User currentUser = (User) redisTemplate.opsForHash().get("token", "user");
        currentUser.setBirthday(birthday);
        redisTemplate.opsForHash().put("token","user",currentUser);
        Integer id = currentUser.getId();

        //调用service层方法修改email
        userService.updateBirthday(birthday,id);

        return Msg.success();
    }

    @RequestMapping(value = "/updatePersonalSign", method = RequestMethod.POST)
    @ResponseBody
    public Msg updatePersonalSign(@RequestParam("personalSign")String personalSign){
        //修改redis缓存，并获取id
        User currentUser = (User) redisTemplate.opsForHash().get("token", "user");
        currentUser.setPersonalSign(personalSign);
        redisTemplate.opsForHash().put("token","user",currentUser);
        Integer id = currentUser.getId();

        //调用service层方法修改email
        userService.updatePersonalSign(personalSign,id);

        return Msg.success();
    }

    @RequestMapping(value = "/updatePersonalIntro", method = RequestMethod.POST)
    @ResponseBody
    public Msg updatePersonalIntro(@RequestParam("personalIntro")String personalIntro){
        //修改redis缓存，并获取id
        User currentUser = (User) redisTemplate.opsForHash().get("token", "user");
        currentUser.setPersonalIntro(personalIntro);
        redisTemplate.opsForHash().put("token","user",currentUser);
        Integer id = currentUser.getId();

        //调用service层方法修改email
        userService.updatePersonalIntro(personalIntro,id);

        return Msg.success();
    }
}
